package team.seekers.catering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.entity.dataobject.Food;
import team.seekers.catering.entity.dataobject.Order;
import team.seekers.catering.entity.vo.info.FoodInfoVO;
import team.seekers.catering.entity.vo.info.OrderInfoVO;
import team.seekers.catering.entity.vo.list.OrderListVO;
import team.seekers.catering.mapper.OrderMapper;
import team.seekers.catering.service.ConfigService;
import team.seekers.catering.service.FoodService;
import team.seekers.catering.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2022-02-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private ConfigService configService;

    @Autowired
    private FoodService foodService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String create(String foodId, String userId) {
        // 线上叫单号查询
        Integer orderNumber = configService.getOrderNumber();
        Order order = new Order();
        order.setFoodId(foodId);
        order.setUserId(userId);
        order.setShowId(orderNumber+1);
        order.setStatus(0);
        save(order);

        configService.addOrderNumber();
        return order.getId();
    }

    @Override
    public OrderInfoVO getToday() {
        OrderInfoVO orderInfoVO = new OrderInfoVO();
        Order order = lambdaQuery().eq(Order::getStatus, 0).eq(Order::getUserId, CateringUtil.getUserId()).one();
        if(order == null){
            return null;
        }
        BeanUtils.copyProperties(order,orderInfoVO);
        FoodInfoVO foodInfoVO = foodService.get(order.getFoodId());
        orderInfoVO.setFoodInfoVO(foodInfoVO);
        return orderInfoVO;
    }

    @Override
    public List<OrderListVO> getTodayOrders() {
        ArrayList<OrderListVO> orderListVOS = new ArrayList<>();
        List<Order> list = lambdaQuery().eq(Order::getStatus, 0).list();
        for (Order order : list) {
            OrderListVO orderListVO = new OrderListVO();
            orderListVO.setId(order.getId());
            FoodInfoVO foodInfoVO = foodService.get(order.getFoodId());
            orderListVO.setFoodName(foodInfoVO.getName());
            orderListVO.setShowId(order.getShowId());
            orderListVOS.add(orderListVO);
        }
        return orderListVOS;
    }

    @Override
    public List<OrderListVO> getOldOrders() {
        ArrayList<OrderListVO> orderListVOS = new ArrayList<>();
        List<Order> list = lambdaQuery().eq(Order::getStatus, 1).eq(Order::getUserId, CateringUtil.getUserId()).list();
        for (Order order : list) {
            OrderListVO orderListVO = new OrderListVO();
            orderListVO.setId(order.getId());
            FoodInfoVO foodInfoVO = foodService.get(order.getFoodId());
            if(order.getEndDate() != null){
                orderListVO.setEndDate(new SimpleDateFormat("yyyy-MM-dd").format(order.getEndDate()));
            }
            orderListVO.setFoodName(foodInfoVO.getName());
            orderListVO.setShowId(order.getShowId());
            orderListVOS.add(orderListVO);
        }
        return orderListVOS;
    }

    @Override
    public boolean isExist(String orderId) {
        return getById(orderId) != null;
    }

    @Override
    public void end(String orderId) {
        Order order = new Order();
        order.setId(orderId);
        order.setStatus(1);
        order.setEndDate(new Date());
        updateById(order);
    }
}
